import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import qs from 'qs';
Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    //内容类型 0 首页，1，初中试题，2，高中试题
    content_type:0,
    subjects: [],
    types: [],
    //连接地址
    baseurl: 'http://localhost:5000/',
    //左侧推荐列表
    leftrecommend: [],
    //相似试卷
    similartis: [],
    //当前查看题目详情
    timu: {},
    //查询题目列表
    timulist: [],
    //翻页页码
    pageno: 1,
    //每页尺寸
    pagesize: 10,
    totalpage: 10,
    //初中分类
    pt_chuzhong: [],
    //高中分类
    pt_gaozhong: [],
    //显示的年级类型
    pt_grade:[],
    //科目
    pt_channel: [],
    //年份
    pt_year: [],
    //地域
    pt_area: [],
    //试卷类型
    pt_type: [],

    //选中年级
    check_grade: '0',
    //选中科目
    check_channel: '0',
    //选中年份
    check_year: '0',
    //选中区域
    check_area: '0',
    //选中试卷类型
    check_papertype: '0'


  },
  actions: {
    LOAD_SIMILARTIS: function ({commit, state}, {tid}) {
      axios.get(state.baseurl + "simlartimus?" + tid).then(
        (response) => {
          commit('INIT_SIMILAR_TIMU_LIST', {paperlist: response.data.data});
        }, (err) => {
          console.log(err);
        }
      )

    },
    LOAD_PAPERTYPE: function ({commit, state}) {
      axios.get(state.baseurl + "papertyperel").then((response) => {
        console.log(response.data.data)
        var chuzhong = response.data.data.chuzhong;
        var gaozhong = response.data.data.gaozhong;
        var channel = response.data.data.channel;
        var year = response.data.data.year;
        var area = response.data.data.area;
        var ptype=response.data.data.ptype;
        commit('SET_PAPERTYPE', {
          pt_chuzhong: chuzhong,
          pt_gaozhong: gaozhong,
          pt_channel: channel,
          pt_year: year,
          pt_area: area,
          pt_type:ptype,

        })
      })
    },

    LAOD_TITYPE: function ({commit, state}, {subject}) {
      axios.get(state.baseurl + "subjecttyperel?" + subject).then(
        (response) => {
          console.log(response.data.data)

          commit('INIT_TI_TYPE', {subjects: response.data.data})
        }, (err) => {
          console.log(err);
        }
      )
    },

    LOAD_LEFT_RECOMMED: function ({commit, state}) {
      axios.get(state.baseurl + 'leftrecommend?subject=' + state.checksubject + '&type=' + state.checktitype)
        .then((response) => {
          commit('INIT_LEFT_RECOMMEND', {leftrecommend: response.data})
        }, (err) => {
          console.log(err);
        })
    },
    LOAD_TIMU_LIST: function ({commit, state}) {
      var data={paperyear:state.check_year,paperarea:state.check_area,channel:state.check_channel,grade:state.check_grade,papertype:state.check_papertype,pageno:state.pageno,pagesize:state.pagesize}
      var config={
        method:'post',
        data:qs.stringify(data)
      };
      console.log(data);
      axios.post(state.baseurl+'tipage',qs.stringify(data))
        .then((response) => {
          commit('INIT_TIMU_LIST', {timulist: response.data.data.result,totalpage:response.data.data.totalpage})
        }, (err) => {
          console.log(err);
        })
    },
    LOAD_TI_DETAIL: function ({commit, state}, {tid}) {
      axios.get(state.baseurl + 'tidetail?id=' + tid)
        .then((response) => {
          commit('INIT_TI_DETAIL', {tiddetail: response.data.data})
        }, (err) => {
          console.log(err);
        })
    },
    SEARCH_TIMU_LIST: function ({commit, state}, key, pageno) {
      axios.post(state.baseurl + '/c.do?searchKey=' + key + '&&pageno=' + pageno)
        .then((response) => {
          commit('INIT_SEARCH_TIMU_LIST', {timulist: response.timulist})
        }, (err) => {
          console.log(err)
        })
    }
  },
  mutations: {
    //设置当前选择的科目和题型
    SET_CHECK_SUBJECT: (state, {subject}) => {
      state.checksubject = subject;
    },
    SET_PAGE: (state, {pageno}) => {
      state.pageno = pageno;
    },

    SET_CHECK_TITYPE: (state, {titype}) => {
      state.checktitype = titype;
    },

    SET_TYPE: (state, {types}) => {
      state.types = types;
    },

    INIT_TI_TYPE: (state, {subjects}) => {
      console.log("subjects:")
      console.log(subjects);
      state.subjects = subjects;
    },

    SET_PAPERTYPE: (state, {pt_chuzhong, pt_gaozhong, pt_channel, pt_area, pt_year,pt_type}) => {
      //初中
      state.pt_chuzhong = pt_chuzhong;
      //高中
      state.pt_gaozhong = pt_gaozhong;
      //区域
      state.pt_area = pt_area;
      //科目
      state.pt_channel = pt_channel;
      //年份
      state.pt_year = pt_year;
      //试卷类型
      state.pt_type=pt_type;

    },


    INIT_CHECK_PAPER: (state, {check_channel, check_grade, check_papertype, check_area, check_year}) => {
      if (check_channel != null) {
        state.check_channel = check_channel;
      }
      if(check_grade!=null){
        state.check_grade=check_grade
      }
      if(check_papertype!=null){
        state.check_papertype=check_papertype;
      }
      if(check_area!=null){
        state.check_area=check_area;
      }
      if(check_year!=null){
        state.check_year=check_year;
      }
       /* state.check_grade = check_grade;
        state.check_area = check_area;
        state.check_year = check_year;
        state.check_papertype = check_papertype;*/

    }
    ,
    SET_CONTENT_TYPE:(state,{content_type})=>{
      state.content_type=content_type;
      if(content_type==0){
        state.pt_grade=[];

      }
      else if(content_type==1)
      {
        state.pt_grade=state.pt_chuzhong;

      }
      else if (content_type==2)
      {

        state.pt_grade=state.pt_gaozhong;
      }

    },
    //初始化左侧推荐列表
    INIT_LEFT_RECOMMEND: (state, {leftrecommend}) => {
      state.leftrecommend = leftrecommend;
    },
    //初始化题目详情
    INIT_TI_DETAIL: (state, {tiddetail}) => {
      state.timu = tiddetail;
    },
    //初始化题目列表
    INIT_TIMU_LIST: (state, {timulist,totalpage}) => {
      state.timulist = timulist;
      state.totalpage=totalpage;
    },
    //初始化关键字搜索列表
    INIT_SEARCH_TIMU_LIST: (state, {timulist}) => {
      state.timulist = timulist;
    },
    INIT_SIMILAR_TIMU_LIST: (state, {paperlist}) => {
      state.similartis = paperlist;
    }
  },
  getters: {
    left_recommends: state => {
      return state.leftrecommend;
    },
    ti_detail: state => {
      return state.timu;
    },
    ti_list: state => {
      return state.timulist;
    }


  }
});
export default store
